# Contributor: Mehdi Chinoune <mehdi.chinoune@hotmail.com>

_realname=qt6-multimedia
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}"
         "${MINGW_PACKAGE_PREFIX}-${_realname}-debug")
_qtver=6.4.2
pkgver=${_qtver/-/}
pkgrel=1
arch=(any)
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://www.qt.io'
license=(GPL3 LGPL3 FDL custom)
pkgdesc='Classes for audio, video, radio and camera functionality (mingw-w64)'
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-qt6-declarative"
             "${MINGW_PACKAGE_PREFIX}-qt6-shadertools"
             "${MINGW_PACKAGE_PREFIX}-qt6-quick3d"
             "${MINGW_PACKAGE_PREFIX}-ffmpeg"
            #  "${MINGW_PACKAGE_PREFIX}-gst-plugins-base"
            #  "${MINGW_PACKAGE_PREFIX}-mesa"
            #  "${MINGW_PACKAGE_PREFIX}-pulseaudio"
             "rsync")
options=('!strip')
_pkgfn="${_realname/6-/}-everywhere-src-${_qtver}"
source=("https://download.qt.io/official_releases/qt/${pkgver%.*}/${_qtver}/submodules/${_pkgfn}.tar.xz"
        001-export-some-constexpr-variables.patch)
sha256sums=('7f2b70deeada911c8e660e2801286657f297a5d1d543d1f6bfa856f28972c776'
            'e1694338779eb499341618c62d11f832f2b7f1f90c8a20345932530a61203105')

prepare() {
  cd "${srcdir}"/${_pkgfn}
  if [[ ${MINGW_PACKAGE_PREFIX} != *-clang-* ]]; then
    patch -p1 -i "${srcdir}"/001-export-some-constexpr-variables.patch
  fi
}

build() {
  [[ -d build-${MSYSTEM} ]] && rm -rf build-${MSYSTEM}
  mkdir -p build-${MSYSTEM} && cd build-${MSYSTEM}

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    --log-level=STATUS \
    -GNinja \
    -DCMAKE_BUILD_TYPE=RelWithDebInfo \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DFEATURE_wmf=ON \
    ../${_pkgfn}

  ${MINGW_PREFIX}/bin/cmake --build .
}

package_qt6-multimedia() {
  depends=("${MINGW_PACKAGE_PREFIX}-qt6-base"
           "${MINGW_PACKAGE_PREFIX}-qt6-shadertools")
  optdepends=("${MINGW_PACKAGE_PREFIX}-qt6-declarative"
              "${MINGW_PACKAGE_PREFIX}-qt6-quick3d"
              "${MINGW_PACKAGE_PREFIX}-ffmpeg")
  groups=("${MINGW_PACKAGE_PREFIX}-qt6")

  DESTDIR=${pkgdir} ${MINGW_PREFIX}/bin/cmake --install build-${MSYSTEM}

  # Seperate debug-info files
  rsync -armR --remove-source-files --include="*/" --include="*.debug" --exclude="*" --prune-empty-dirs ${pkgdir}/.${MINGW_PREFIX} ${srcdir}/${MSYSTEM}-debug/

  install -d "$pkgdir${MINGW_PREFIX}"/share/licenses/${_realname}
  install -Dm644 $_pkgfn/LICENSES/* -t "$pkgdir${MINGW_PREFIX}"/share/licenses/${_realname}
}

package_qt6-multimedia-debug() {
  depends=("${MINGW_PACKAGE_PREFIX}-qt6-base-debug"
           "${MINGW_PACKAGE_PREFIX}-qt6-shadertools-debug"
           "${MINGW_PACKAGE_PREFIX}-${_realname}")
  groups=("${MINGW_PACKAGE_PREFIX}-qt6-debug")

  cp -rf ${srcdir}/${MSYSTEM}-debug${MINGW_PREFIX} "${pkgdir}"/
}

# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;
